#-*- coding:gb18030 -*-
#$Id: exception.py 32816 2008-08-29 08:56:57Z jorwen@NETEASE.COM $

from django.http import HttpResponse
from django.shortcuts import render_to_response
import settings


class V4aException(Exception):

	def __init__(self, err_msg):
		self.err_msg = err_msg
		self.template_name = None

	def what(self):
		return self.err_msg

	def template(self):
		return self.template_name


class V4aAdminException(V4aException):

	def __init__(self, err_msg):
		V4aException.__init__(self, err_msg)
		self.template_name = 'v4a/admin_exception.html'


class V4aVotingException(V4aException):
	def __init__(self, err_msg):
		V4aException.__init__(self, err_msg)
		self.template_name = 'v4a/voting_exception.html'


class V4aExceptionMiddleware(object):

	def process_exception(self, request, exception):

		if isinstance(exception, V4aException):
			return render_to_response(exception.template(), {'errmsg':exception.what()}) 

		else:
			if settings.DEBUG:
				raise
			else:
				return HttpResponse('Exception happened!')


